package com.mialab.servlet_demo;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/GetResourceServlet")
public class GetResourceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public GetResourceServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		ServletContext context = this.getServletContext();
		// 获取文件绝对路径
		String path = context.getRealPath("/WEB-INF/classes/data.properties");
		FileInputStream in = new FileInputStream(path);
		Properties pros = new Properties();
		pros.load(in);
		out.println("username = " + pros.getProperty("username") + "<br>");
		out.println("password = " + pros.getProperty("password") + "<br>");
		out.println("driverClassName = " + pros.getProperty("driverClassName") + "<br>");
		out.println("url = " + pros.getProperty("url") + "<br>");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
